/*
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.list.storage;

import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.RandomAccess;
import org.tiberiumlabs.lailaps.list.DownloadItem;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class ListWriter implements ListAttributes {

    private final String LS = System.getProperty("line.separator");

    private final String PARAM_START = "=\"";
    private final String PARAM_END = "\" ";
    private final String TAG_START = "    <item ";
    private final String TAG_END = "/>";

    private final void writeItem(DownloadItem item, Writer out) throws IOException {

        out.write(TAG_START);

        out.append(URL).append(PARAM_START).append(item.getUrl()).append(PARAM_END);
        out.append(OUTPUT_FILE).append(PARAM_START).append(item.getOutputFile()).append(PARAM_END);
        String path = item.getOutputPath();
        if (path != null) {
            out.append(OUTPUT_PATH).append(PARAM_START).append(path).append(PARAM_END);
        }
        long size = item.getFullSize();
        if (size > 0) {
            out.append(FULL_SIZE).append(PARAM_START).append(String.valueOf(size)).append(PARAM_END);
        }
        size = item.getCompletedSize();
        if (size > 0) {
            out.append(COMPLETED_SIZE).append(PARAM_START).append(String.valueOf(size)).append(PARAM_END);
        }
        if (item.isTargetFileCreated()) {
            out.append(FILE_CREATED).append(PARAM_START).append(TRUE).append(PARAM_END);
        }
        if (item.getStatus() == COMPLETED) {
            out.append(STATUS).append(PARAM_START).append(TRUE).append(PARAM_END);
        }
        out.append(TIME_CREATED).append(PARAM_START).append(String.valueOf(item.getCreatedTime())).append(PARAM_END);
        if (item.getCompletedTime() != -1) {
            out.append(TIME_COMPLETED).append(PARAM_START).append(String.valueOf(item.getCompletedTime())).append(PARAM_END);
        }

        out.write(TAG_END);
        out.write(LS);
    }

    public final void writeList(List<DownloadItem> list, Writer out, String encoding) throws IOException {

        int listSize = list.size();

        out.write("<?xml version=\"1.0\" encoding=\"");
        out.write(encoding);
        out.write("\"?>");
        out.write(LS);
        out.write("<list size=\"");
        out.write(String.valueOf(listSize));
        out.write("\" >");
        out.write(LS);

        if (list instanceof RandomAccess) {
            for (int i = 0; i < listSize; i++) {
                writeItem(list.get(i), out);
            }
        }
        else {
            Iterator<DownloadItem> iterator = list.iterator();
            while (iterator.hasNext()) {
                writeItem(iterator.next(), out);
            }
        }

        out.write("</list>");
        out.write(LS);
        out.flush();
    }

}
